from playwright.sync_api import sync_playwright
import time
import redis
import pymysql
import json

import redis
redis_con = redis.Redis(host='121.4.69.146', password='xhhg123456',
                        port=6389, db=2, decode_responses=True)

conn = pymysql.connect(host="localhost",
                       user='root',
                       port=3306,
                       password='123456',
                       database='big_data')
cur = conn.cursor()


def select_gonkaihao():
    sql = r'''SELECT distinct gonkaihao FROM `agent_gonkaihao` WHERE
gonkaihao not in (SELECT distinct gonkaihao FROM pantent_status_gkh )'''
    cur.execute(sql)
    data = cur.fetchall()
    gkaihao_list = [sqh[0] for sqh in data]
    return gkaihao_list


def run(inst):
    with sync_playwright() as p:
        # browser = p.firefox.launch(headless=False)
        import json
        storage_state_path = f'./auth/{inst}_state.json'
        with open(storage_state_path) as file:
            storage_state = json.load(file)
            origins = storage_state.get('origins')
            local_stroge_list = [item.get('localStorage') for item in origins]
            for local_stroge in local_stroge_list:
                for item in local_stroge:
                    if item.get('name') == 'ACCESS_TOKEN':
                        authorization = item.get('value')
        print(f'成功读取{inst}登录信息:{authorization}')
        browser = p.firefox.launch()
        context = browser.new_context(
            base_url='https://cpquery.cponline.cnipa.gov.cn', storage_state=storage_state_path)
        context.set_default_timeout(600000)
        context.set_default_navigation_timeout(600000)
        page = context.new_page()
        page.goto(
            'https://cpquery.cponline.cnipa.gov.cn/chinesepatent/index')
        page.wait_for_load_state('networkidle')
        while redis_con.scard('tmp_外部接口查询'):
            sqh = redis_con.spop('tmp_外部接口查询')
            start_time = time.time()
            api_request_context = context.request
            headers = {
                "Accept": "application/json, text/plain, */*",
                "Content-Type": "application/json;charset=utf-8",
                "Authorization": 'Bearer ' + authorization,
                "userType": "USER_RZ_DAILIJIG"
            }
            data = {
                "zhuanlilx": "",
                "page": 1,
                "size": 10,
                "sortDataName": "",
                "sortType": "",
                'zhuanlisqh': sqh
            }
            # sqxx,fyxx
            response = api_request_context.post(
                '/api/search/undomestic/publicSearch', data=data, headers=headers, timeout=0)
            res_data = response.json()
            try:
                gk_rq = res_data.get('data').get(
                    'records')[0].get('gongkaiggr')
            except:
                gk_rq = ''
            print(f'{sqh}:{gk_rq}')
            import csv
            with open('tmp_sqh.csv', 'a+', newline='', encoding='utf-8') as file:
                csv_file = csv.writer(file)
                csv_file.writerow([sqh, gk_rq])
            page.reload()
            page.wait_for_load_state('networkidle')
            # if sqh:
            #     sqxx_response = api_request_context.post(
            #         '/api/view/gn/sqxx', data={'zhuanlisqh': sqh}, headers=headers)
            #     patent_status_res_json = sqxx_response.json()
            #     patent_status_data = patent_status_res_json.get('data')
            #     item = dict()
            #     item["sqh"] = sqh
            #     try:
            #         item["state"] = patent_status_data.get(
            #             'zhuluxmxx').get('zhuluxmxx').get('anjianywzt')
            #     except:
            #         item["state"] = ''
            #     try:
            #         item["name"] = patent_status_data.get(
            #             'zhuluxmxx').get('zhuluxmxx').get('zhuanlimc')
            #     except:
            #         item["name"] = ''
            #     try:
            #         item["sq_date"] = patent_status_data.get(
            #             'zhuluxmxx').get('zhuluxmxx').get('shenqingr')
            #     except:
            #         item["sq_date"] = ''
            #     try:
            #         dljg_info = patent_status_data.get('dailijg').get('dailijgList')[-1]
            #         item["dljg"] = dljg_info.get('dailijgdm')
            #         item["dlr"] = dljg_info.get('diyidlrxm')
            #     except:
            #         item["dljg"] = ''
            #         item["dlr"] = ''
            #     famingrenList = patent_status_data.get(
            #         'famingren').get('famingrenList')
            #     try:
            #         sqr_list = [line.get('famingrxm') for line in famingrenList]
            #     except:
            #         sqr_list = []
            #     try:
            #         shenqingrenList = patent_status_data.get(
            #             'shenqingren').get('shenqingrenList')
            #         item["sqr"] = ','.join([line.get('shenqingrxm')
            #                                 for line in shenqingrenList])
            #     except:
            #         item["sqr"] = ''
            #     item["fmr_name"] = ','.join(sqr_list)
            #     item['inst'] = inst
            #     try:
            #         item['fm_class'] = patent_status_data.get(
            #             'zhuluxmxx').get('zhuluxmxx').get('zhufenlh')
            #     except:
            #         item['fm_class'] = ''
            #     print(item)
            #     print('====' * 10)
            #     sql = f"""INSERT INTO pantent_status_gkh (gonkaihao,gk_rq,application_number, case_status, fm_name,fm_class, sq_date,  agency, patent_user, first_agent, fmr) VALUES ('{gonkaihao}','{gk_rq}','{item["sqh"]}', '{item["state"]}', '{item["name"]}','{item["fm_class"]}',  '{item["sq_date"]}', '{item["dljg"]}', '{item["sqr"]}', '{item["dlr"]}', '{item["fmr_name"]}')"""
            #     print(sql)
            #     cur.execute(sql)
            #     conn.commit()
            # page.reload()


if __name__ == '__main__':
    from multiprocessing.pool import Pool
    inst = 'WB'
    # run(inst)
    p = Pool(6)
    for i in range(6):
        p.apply_async(run, args=(inst,))
    p.close()
    p.join()
    conn.close()
